﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Jiuyong
{
    public static class Config<T>
            where T:class,new()
    {
        public static T Load()
        {
            T r;//= new T()
            var ct = typeof(T);
            var fn = String.Format("{0}.xml", ct);
             if (File.Exists(fn))
            {
                var srz = new System.Xml.Serialization.XmlSerializer(ct);
                var fs = new FileStream(ct.ToString() + ".xml", FileMode.Open, FileAccess.Read);
                r = srz.Deserialize(fs) as T;
            }
             else
             {
                 r = new T();
             }
            return r;
        }

        public static void Save(T config)
        {
            var ct = config.GetType();
            var srz = new System.Xml.Serialization.XmlSerializer(ct);
            var fs = new FileStream(ct.ToString()+".xml",FileMode.OpenOrCreate,FileAccess.Write);
            srz.Serialize(fs, config);
            fs.Close();
        }
    }
}
